package com.xiaode.controller.admin.selfStudyRoom;

import java.util.List;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xiaode.common.model.Camera;
import com.xiaode.common.model.MonitorClassroom;
import com.xiaode.exce.EnumError;
import com.xiaode.util.hikvision.HikvisionUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;

public class SelfStudyService {
	/**
	 * 获取监控点（教室）列表
	 */
	public Ret getMonitorClassroomList() throws Exception {
		List<MonitorClassroom> monitorClassroomList = MonitorClassroom.dao.findAll();
		for(MonitorClassroom monitorClassroom : monitorClassroomList) {
			Integer count = Db.queryInt("SELECT COUNT(*) count FROM camera WHERE monitor_classroon_id = ?", monitorClassroom.getId());
			monitorClassroom.put("cameraNumber", count);
		}
		return Ret.ok("monitorClassroomList", monitorClassroomList);
	}
	
	/**
	 * 删除监控教室
	 * 删除后，需要删除对应的摄像头绑定关系，以及教室中的人
	 */
	@Before(Tx.class)
	public Ret deleteMonitorClassroom(Integer monitorClassroomId) throws Exception {
		int i = Db.delete("DELETE FROM monitor_classroom WHERE id = ?", monitorClassroomId);
		if(i > 0) {
			Db.delete("DELETE FROM monitor_classroom_user WHERE monitor_classroom_id = ?", monitorClassroomId);
			Db.update("UPDATE camera SET monitor_classroon_id = NULL, use_type = NULL WHERE monitor_classroon_id = ?", monitorClassroomId);
		}
		return Ret.ok();
	}
	
	/**
	 * 添加监控教室
	 */
	@Before(Tx.class)
	public Ret addMonitorClassroom(String pictureUrl, String name, String cameraIds) {
		MonitorClassroom monitorClassroom = MonitorClassroom.dao.findFirst("SELECT * FROM monitor_classroom WHERE name = ?", name);
		if(BeanUtil.isEmpty(monitorClassroom)) {
			return Ret.fail().set("error", EnumError.HAVE_CLASSROOM_NAME.getObj());
		}
		monitorClassroom = new MonitorClassroom(); 
		monitorClassroom.setPicture(pictureUrl);
		monitorClassroom.setName(name);
		monitorClassroom.save();
		List<Integer> cameraIdList = JSONUtil.toList(cameraIds, Integer.class);
		if(cameraIdList.size() > 0) {
			Db.template("updateCameraClassroom", Kv.of("cameraIdList", cameraIdList).set("monitorClassroomId", monitorClassroom.getId())).update();
		}
		return Ret.ok();
	}
	
	/**
	 * 获取监控教室配置详情
	 */
	public Ret getMonitorClassroom(Integer monitorClassroomId) {
		MonitorClassroom monitorClassroom = MonitorClassroom.dao.findById(monitorClassroomId);
		if(BeanUtil.isEmpty(monitorClassroom)) {
			return Ret.fail().set("error", EnumError.MONITOR_CLASSROOM.getObj());
		}
		List<Camera> cameraNoBindingList = Camera.dao.find("SELECT * FROM camera WHERE monitor_classroon_id IS NULL");
		List<Camera> cameraList = Camera.dao.find("SELECT * FROM camera WHERE monitor_classroon_id = ?", monitorClassroomId);
		monitorClassroom.put("cameraNoBindingList", cameraNoBindingList).put("cameraList", cameraList);
		
		return Ret.ok("monitorClassroom", monitorClassroom);
	}
	
	/**
	 * 修改监控教室
	 */
	@Before(Tx.class)
	public Ret updateMonitorClassroom(Integer monitorClassroomId, String pictureUrl, String cameraIds) {
		MonitorClassroom monitorClassroom = MonitorClassroom.dao.findById(monitorClassroomId);
		if(BeanUtil.isEmpty(monitorClassroom)) {
			return Ret.fail().set("error", EnumError.MONITOR_CLASSROOM.getObj());
		}
		monitorClassroom.setPicture(pictureUrl);
		monitorClassroom.update();
		//先清掉
		Db.update("UPDATE camera SET monitor_classroon_id = NULL, use_type = NULL WHERE monitor_classroon_id = ?", monitorClassroomId);
		//再重新加
		List<Integer> cameraIdList = JSONUtil.toList(cameraIds, Integer.class);
		if(cameraIdList.size() > 0) {
			Db.template("updateCameraClassroom", Kv.of("cameraIdList", cameraIdList).set("monitorClassroomId", monitorClassroom.getId())).update();
		}
		return Ret.ok();
	}
	
	
	/**
	 * 获取教室摄像头监控流
	 */
	public Ret getPreviewURLs(Integer monitorClassroomId) throws Exception {
		MonitorClassroom monitorClassroom = MonitorClassroom.dao.findById(monitorClassroomId);
		if(BeanUtil.isEmpty(monitorClassroom)) {
			return Ret.fail().set("error", EnumError.MONITOR_CLASSROOM.getObj());
		}
		List<Camera> cameraList = Camera.dao.find("SELECT * FROM camera WHERE monitor_classroon_id = ?", monitorClassroomId);
		for(Camera camera : cameraList) {
			String url = HikvisionUtil.getPreviewURLs(camera.getCameraCode());
			camera.put("url", url);
		}
		return Ret.ok("cameraList", cameraList).set("monitorClassroom", monitorClassroom);
	}
	
	/**
	 * 获取某个摄像头回放流
	 */
	public Ret getPlaybackURLs(Integer monitorClassroomId, String beginTime, String endTime) throws Exception {
		List<Camera> cameraList = Camera.dao.find("SELECT * FROM camera WHERE monitor_classroon_id = ?", monitorClassroomId);
		for(Camera camera : cameraList) {
			String url = HikvisionUtil.getPlaybackURLs(camera.getCameraCode(), beginTime, endTime);
			camera.put("url", url);
		}
		return Ret.ok("cameraList", cameraList);
	}
	
	/**
	 * 获取某个摄像头回放流
	 */
	public Ret getPlaybackURL(Integer cameraId, String beginTime, String endTime) throws Exception {
		Camera camera = Camera.dao.findById(cameraId);
		String url = HikvisionUtil.getPlaybackURLs(camera.getCameraCode(), beginTime, endTime);
		return Ret.ok("url", url);
	}

	/**
	 * 获取没有关联教室的摄像头
	 */
	public Ret getNoBindingCamera() throws Exception {
		List<Camera> cameraList = Camera.dao.find("SELECT * FROM camera WHERE monitor_classroon_id IS NULL");
		return Ret.ok("cameraList", cameraList);
	}

	/**
	 * 删除摄像头
	 */
	public Ret deleteCamera(Integer cameraId) throws Exception {
		Camera camera = Camera.dao.findById(cameraId);
		if(BeanUtil.isNotEmpty(camera)) {
			camera.delete();
		}
		return Ret.ok();
	}

	
	
	
}

